using UnityEngine;

public class SceneLightmapData : MonoBehaviour
{
    [System.Serializable]
    public struct LightingInfo
    {
        [SerializeField]
        public Texture2D lightmapDir;
        [SerializeField]
        public Texture2D Lightmap;
        [SerializeField]
        public Texture2D shadowMask;
        public LightingInfo(Texture2D lightmapDir, Texture2D Lightmap, Texture2D shadowMask)
        {
            this.lightmapDir = lightmapDir;
            this.Lightmap = Lightmap;
            this.shadowMask = shadowMask;
        }
    }

    [SerializeField]
    public LightingInfo[] m_lightingInfos;
    [SerializeField]
    public LightProbes LightProbes;
    [SerializeField]
    public LightmapsMode lightmapsMode;
    [SerializeField]
    public Material skybox;
    [SerializeField]
    public UnityEngine.Rendering.AmbientMode ambientMode;
    [SerializeField]
    public Color ambientSkyColor;
    [SerializeField]
    public Color ambientEquatorColor;
    [SerializeField]
    public Color ambientGroundColor;
    [SerializeField]
    public UnityEngine.Rendering.DefaultReflectionMode defaultReflectionMode;
    [SerializeField]
    //public Cubemap customReflection;
    public Texture customReflection;
    [SerializeField]
    public int reflectionBounces;
    [SerializeField]
    public float reflectionIntensity;
    [SerializeField]
    public float fogDensity;
    [SerializeField]
    public Color fogColor;
    [SerializeField]
    public FogMode fogMode;
    [SerializeField]
    public float fogEndDistance;
    [SerializeField]
    public float fogStartDistance;
    [SerializeField]
    public bool fog;

    private Material m_prev_skybox;
    private UnityEngine.Rendering.AmbientMode m_prev_ambientmode;
    private Color m_prev_ambientSkyColor;
    private Color m_prev_ambientEquatorColor;
    private Color m_prev_ambientGroundColor;
    private UnityEngine.Rendering.DefaultReflectionMode m_prev_defaultReflectionMode;
    //private Cubemap m_prev_customReflection;
    private Texture m_prev_customReflection;
    private LightmapData[] m_prev_lightmaps;
    private LightProbes m_prev_lightProbes;
    private LightmapsMode m_prev_lightmapsMode;
    private int m_prev_reflectionBounces;
    private float m_prev_reflectionIntensity;

    private float m_prev_fogDensity;
    private Color m_prev_fogColor;
    private FogMode m_prev_fogMode;
    private float m_prev_fogEndDistance;
    private float m_prev_fogStartDistance;
    private bool m_prev_fog;

    private void OnEnable()
    {
        m_prev_skybox = RenderSettings.skybox;
        m_prev_ambientmode = RenderSettings.ambientMode;
        m_prev_ambientSkyColor = RenderSettings.ambientSkyColor;
        m_prev_ambientEquatorColor = RenderSettings.ambientEquatorColor;
        m_prev_ambientGroundColor = RenderSettings.ambientGroundColor;
        m_prev_defaultReflectionMode = RenderSettings.defaultReflectionMode;
#if UNITY_2022_OR_NEWER
        m_prev_customReflection = RenderSettings.customReflectionTexture;
#else
        m_prev_customReflection = RenderSettings.customReflection;
#endif
        m_prev_reflectionBounces = RenderSettings.reflectionBounces;
        m_prev_reflectionIntensity = RenderSettings.reflectionIntensity;
        m_prev_fogDensity = RenderSettings.fogDensity;
        m_prev_fogColor = RenderSettings.fogColor;
        m_prev_fogMode = RenderSettings.fogMode;
        m_prev_fogEndDistance = RenderSettings.fogEndDistance;
        m_prev_fogStartDistance = RenderSettings.fogStartDistance;
        m_prev_fog = RenderSettings.fog;

        m_prev_lightmaps = LightmapSettings.lightmaps;
        m_prev_lightProbes = LightmapSettings.lightProbes;
        m_prev_lightmapsMode = LightmapSettings.lightmapsMode;

        RenderSettings.skybox = skybox;
        RenderSettings.ambientMode = ambientMode;
        RenderSettings.ambientSkyColor = ambientSkyColor;
        RenderSettings.ambientEquatorColor = ambientEquatorColor;
        RenderSettings.ambientGroundColor = ambientGroundColor;
        RenderSettings.defaultReflectionMode = defaultReflectionMode;
        RenderSettings.customReflection = customReflection;
        RenderSettings.reflectionBounces = reflectionBounces;
        RenderSettings.reflectionIntensity = reflectionIntensity;

        if (fog)
        {
            RenderSettings.fogDensity = fogDensity;
            RenderSettings.fogColor = fogColor;
            RenderSettings.fogMode = fogMode;
            RenderSettings.fogEndDistance = fogEndDistance;
            RenderSettings.fogStartDistance = fogStartDistance;
        }
        RenderSettings.fog = fog;

        var lightmaps = new LightmapData[m_lightingInfos.Length];

        for (int i = 0; i < lightmaps.Length; i++)
        {
            lightmaps[i] = new LightmapData();
            lightmaps[i].lightmapDir = m_lightingInfos[i].lightmapDir;
            lightmaps[i].lightmapColor = m_lightingInfos[i].Lightmap;
            lightmaps[i].shadowMask = m_lightingInfos[i].shadowMask;
        }
        LightmapSettings.lightmaps = lightmaps;
        LightmapSettings.lightProbes = LightProbes;
        LightmapSettings.lightmapsMode = lightmapsMode;
    }

    private void OnDisable()
    {
        RenderSettings.skybox = m_prev_skybox;
        RenderSettings.ambientMode = m_prev_ambientmode;
        RenderSettings.ambientSkyColor = m_prev_ambientSkyColor;
        RenderSettings.ambientEquatorColor = m_prev_ambientEquatorColor;
        RenderSettings.ambientGroundColor = m_prev_ambientGroundColor;
        RenderSettings.defaultReflectionMode = m_prev_defaultReflectionMode;
#if UNITY_2022_OR_NEWER
        RenderSettings.customReflectionTexture = m_prev_customReflection;
#else
        RenderSettings.customReflection = m_prev_customReflection;
#endif
        RenderSettings.reflectionBounces = m_prev_reflectionBounces;
        RenderSettings.reflectionIntensity = m_prev_reflectionIntensity;

        RenderSettings.fogDensity = m_prev_fogDensity;
        RenderSettings.fogColor = m_prev_fogColor;
        RenderSettings.fogMode = m_prev_fogMode;
        RenderSettings.fogEndDistance = m_prev_fogEndDistance;
        RenderSettings.fogStartDistance = m_prev_fogStartDistance;
        RenderSettings.fog = m_prev_fog;

        LightmapSettings.lightmaps = m_prev_lightmaps;
        LightmapSettings.lightProbes = m_prev_lightProbes;
        LightmapSettings.lightmapsMode = m_prev_lightmapsMode;
    }

    public void Update()
    {

    }
}